<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
    request.setCharacterEncoding("UTF-8");
    String htmlData = request.getParameter("content1") != null ? request.getParameter("content1") : "";
%>
<!doctype html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>部门管理</title>
    <jsp:include page="../../head.jsp"/>
    <style type="text/css">
        .td_right {
            text-align: right;
        }
    </style>
    <script>
        $(function () {
            //初始化表单验证引擎
            $("#deptForm").validationEngine("attach", {
                //消息位置
                promptPosition: "bottomRight",
                scroll: false
            });
            //初始化ajaxForm参数
            var options = {
                //提交方式
                type: "POST",
                //提交数据格式
                dataType: "json",
                //在提交之前做的操作
                beforeSubmit: function () {
                    return $("#deptForm").validationEngine("validate");
                },
                //成功后做的操作
                success: function (responseText, statusText, xhr, $form) {
                    alert(responseText.msg);
                    if (responseText.result) {
                        location.href = "company-mainDept?companyId=" + $("#companyId").val();
                    }
                }
            };
            $('#deptForm').ajaxForm(options);
        });
        function submitTheForm() {
            $.ajax({
                       url: "company-checkDeptExist",
                       type: "POST",
                       dataType: "json",
                       data: "dept_name=" + $("#dept_name").val(),
                       success: function (data) {
                           if (data != null) {
                               data = $.parseJSON(data["data"]);
                               if (data.state == "1") {
                                   alert(data.msg);
                                   return;
                               }
                           } else {
                               $("#deptForm").submit();
                           }
                       }
                   });
        }

    </script>
</head>
<body>
<%=htmlData%>
<form action="company-saveOrUpdateDept" method="post" id="deptForm">
    <div class="ui-widget">
        <table class="ui-widget-content">
            <tr>
                <td class="td_right">部门名称</td>
                <td><input class="validate[required]" type="text" id="dept_name" name="name"
                           value="${dept.name}"
                           style="width: 150px;"/>*
                </td>
            </tr>
        </table>
    </div>
    <input type="hidden" name="companyId" id="companyId" value="${companyId}"/>
    <input type="hidden" name="id" value="${dept.id}"/>
    <input type="hidden" name="isabled" value="${dept.isabled}"/>
    <input type="hidden" name="state" value="${dept.state}"/>
    <input type="button" value="保存" onclick="submitTheForm();"/>
    <input type="button" value="返回" onclick="javascript:history.back();"/>
</form>
</body>
</html>
